package com.xiaozhen.tcp.util;

import com.xiaozhen.common.core.domain.driver.ParseTcpAttributeFormula;
import com.xiaozhen.common.core.utils.ByteArrayUtil;
import com.xiaozhen.common.core.utils.DateUtils;
import com.xiaozhen.tcp.factory.OperatorFactory;
import com.xiaozhen.tcp.factory.conversion.AryToDecimalConversion;
import com.xiaozhen.tcp.factory.conversion.DecimalToAryConversion;
import lombok.extern.slf4j.Slf4j;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author zhenweisheng
 * @date 2020/9/21 14:42
 * @description:
 */
@Slf4j
public class AttributeValueUtils {

    /**
     * 获取属性信息
     *
     * @param msgInfo    原始数据
     * @param startPoint 开始点位
     * @param endPoint   结束点位
     * @return 字节列表
     */
    public static byte[] getAttributeBytes(byte[] msgInfo, Integer startPoint, Integer endPoint) {
        if (startPoint == null || endPoint == null) {
            return null;
        }
        if (startPoint < 0) {
            startPoint = msgInfo.length + startPoint;
        }
        if (endPoint < 0) {
            endPoint = msgInfo.length + endPoint;
        }
        return getSubString(startPoint, endPoint, msgInfo);
    }

    /**
     * 根据点位获取截取后的字节信息
     *
     * @param startPoint
     * @param endPoint
     * @param msgInfo
     * @return
     */
    private static byte[] getSubString(Integer startPoint, Integer endPoint, byte[] msgInfo) {
        byte[] subBytes = ByteArrayUtil.subBytes(msgInfo, startPoint, endPoint - startPoint);
        return subBytes;
    }

    /**
     * 根据计算得到最终数值
     *
     * @param stringValue
     * @param formulaStepList
     */
    public static String getAttributeValue(Object stringValue, List<ParseTcpAttributeFormula> formulaStepList) {

        //计算结果
        String result = stringValue.toString();
        for (ParseTcpAttributeFormula formulaStep : formulaStepList) {
            //算术符号
            String operate = formulaStep.getOperator();
            String numberA = formulaStep.getNumberA();
            if ("result".equals(numberA)) {
                numberA = result;
            } else if ("this".equals(numberA)) {
                numberA = stringValue.toString();
            }
            String numberB = formulaStep.getNumberB();
            if ("result".equals(numberB)) {
                numberB = result;
            } else if ("this".equals(numberB)) {
                numberB = stringValue.toString();
            }
            if ("r进制转10进制".equals(operate)) {
                AryToDecimalConversion aryToDecimalConversion = new AryToDecimalConversion();
                result = aryToDecimalConversion.conversion(numberA, Integer.parseInt(numberB));
                continue;
            }
            if ("10进制转r进制".equals(operate)) {
                DecimalToAryConversion decimalToAryConversion = new DecimalToAryConversion();
                result = decimalToAryConversion.conversion(Integer.parseInt(numberA), Integer.parseInt(numberB));
                continue;
            }
            if ("r进制浮点数转十进制".equals(operate)) {
                Float intBitsToFloat = Float.intBitsToFloat(Integer.valueOf(numberA, Integer.parseInt(numberB)));
                result = String.valueOf(intBitsToFloat);
                continue;
            }
            if ("16进制浮点数转时间".equals(operate)) {
                LocalDateTime sourceTime = DateUtils.parseStringToLocalDateTime(numberB, DateUtils.YMDHMS1);
                LocalDateTime localDateTime = sourceTime.plusSeconds(Integer.parseInt(numberA, 16));
                result = DateUtils.getLocalDateTimeAsString(localDateTime, DateUtils.YMDHMS1);
                continue;
            }
            OperatorFactory operatorFactory = new OperatorFactory();
            result = operatorFactory.operator(Double.parseDouble(numberA), Double.parseDouble(numberB), operate);
            log.info("--- 第{}步的计算结果为:{}", formulaStep.getStep(), result);
        }
        return result;
    }
}
